<template>
  <BasicForm @register="registerForm" />
</template>
<script lang="ts" name="UserForm" setup>
import { useAttrs } from 'vue';
import { BasicForm, useForm } from '@/components/form/index';
import { formSchema } from '../data/user.data';
import { Api } from '../data/user.api';

const attrs = useAttrs();
//表单配置
const [registerForm, { setProps, resetFields, setFieldsValue, saveOrUpdate, updateSchema }] = useForm({
  rowKey: 'userId',
  saveApi: Api.save,
  updateApi: Api.edit,
  saveHandler: 'baseUserAddHandler',
  labelWidth: 120,
  schemas: formSchema,
  baseColProps: {
    span: 22,
  },
  showActionButtonGroup: false,
});

/**
 * 表单赋值
 * @param data
 */
async function setData(data) {
  await resetFields();
  if (typeof data.record === 'object') {
    await setFieldsValue({
      ...data.record,
    });
  }
  // 隐藏底部时禁用整个表单
  //await setProps({ disabled: !showFooter })
}

/**
 * 提交事件
 */
async function handleSubmit(drawerEmit) {
  await saveOrUpdate(drawerEmit);
}

/**
 * 暴露给父组件调用的方法
 */
defineExpose({
  setData,
  handleSubmit,
});
</script>
